package edu.wtbu.cs.androiddemo;


import static org.junit.Assert.assertEquals;

import android.content.Context;

import androidx.room.Room;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.List;

import edu.wtbu.cs.androiddemo.sqlite.room.AppDatabase;
import edu.wtbu.cs.androiddemo.sqlite.room.User;
import edu.wtbu.cs.androiddemo.sqlite.room.UserDao;

@RunWith(AndroidJUnit4.class)
public class RoomDemoTest {

    private UserDao mWordDao;
    private AppDatabase mDb;

    @Before
    public void createDb() {
        Context context = ApplicationProvider.getApplicationContext();
        // Using an in-memory database because the information stored here disappears when the
        // process is killed.
        mDb = Room.inMemoryDatabaseBuilder(context, AppDatabase.class)
                // Allowing main thread queries, just for testing.
                .allowMainThreadQueries()
                .build();
        mWordDao = mDb.userDao();
    }

    @Test
    public void insertAndGetWord() throws Exception {
        User user = new User();
        user.setFirstName("chende");
        mWordDao.insertAll(user);
        List<User> allWords = mWordDao.getAll();
        System.out.println(allWords.get(0).getFirstName());
        assertEquals(allWords.get(0).getFirstName(), user.getFirstName());
    }

    @After
    public void closeDb() {
        mDb.close();
    }

}
